package com.zzhou.linkinfo.clock.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.MenuItem;

import com.suke.widget.SwitchButton;
import com.zzhou.linkinfo.clock.Consts;
import com.zzhou.linkinfo.clock.R;
import com.zzhou.linkinfo.clock.activity.base.AbstractTitleActivity;
import com.zzhou.linkinfo.clock.adapter.ClockFrequencyAdapter;
import com.zzhou.linkinfo.clock.entity.ClockFrequency;
import com.zzhou.linkinfo.clock.util.ToastShow;

import java.util.ArrayList;

/**
 * 选择打卡频次
 * Created by Carlos on 2019/03/18.
 */
public class ClockFrequencyActivity extends AbstractTitleActivity {
    public static final String FREQUENCY_RESULT = "frequencyResult";
    public static final String SMS_REMIND_RESULT = "smsRemindResult";
    public static final String CHOSEN_FREQUENCY = "chosenFrequency";

    private RecyclerView mFrequencyRecyclerView;
    private SwitchButton mSmsRemind;

    private ClockFrequencyAdapter mFrequencyAdapter;
    private ArrayList<ClockFrequency> mFrequencyData;

    @Override
    protected int attachLayoutRes() {
        return R.layout.clock_acitivity_clock_frequency;
    }

    @Override
    protected void initView() {
        mFrequencyRecyclerView = findViewById(R.id.frequency_recycler_view);
        mSmsRemind = findViewById(R.id.checkbox_sms_remind);

        Bundle bundle = getIntent().getExtras();
        mFrequencyData = (ArrayList<ClockFrequency>) bundle.getSerializable(CHOSEN_FREQUENCY);
        if (mFrequencyData == null) {
            mFrequencyData = initData();
        }

        mFrequencyAdapter = new ClockFrequencyAdapter(this, mFrequencyData);
        mFrequencyRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mFrequencyRecyclerView.setAdapter(mFrequencyAdapter);

        setListener();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            Intent intent = new Intent();
            intent.putExtra(SMS_REMIND_RESULT, mSmsRemind.isChecked());
            intent.putExtra(FREQUENCY_RESULT, mFrequencyAdapter.getFinalFrequencyData());
            setResult(RESULT_OK, intent);
            finish();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    /**
     * 初始化打卡频次列表数据，默认选中每天
     *
     * @return
     */
    private ArrayList<ClockFrequency> initData() {
        ArrayList<ClockFrequency> frequencies = new ArrayList<>();
        // 每天
        ClockFrequency everyDay = new ClockFrequency();
        everyDay.setFrequencyType(Consts.IClockFrequencyType.TYPE_EVERY_DAY);
        everyDay.setName(getString(R.string.clock_frequency_every_day));
        everyDay.setSelected(true);
        frequencies.add(everyDay);
        // 隔天
        ClockFrequency nextDay = new ClockFrequency();
        nextDay.setFrequencyType(Consts.IClockFrequencyType.TYPE_NEXT_DAY);
        nextDay.setName(getString(R.string.clock_frequency_next_day));
        nextDay.setSelected(false);
        frequencies.add(nextDay);
        // 自定义
        ClockFrequency customTitle = new ClockFrequency();
        customTitle.setFrequencyType(Consts.IClockFrequencyType.TYPE_CUSTOM_TITLE);
        customTitle.setName(getString(R.string.clock_frequency_custom));
        customTitle.setSelected(false);
        frequencies.add(customTitle);
        // 每周一
        ClockFrequency customMon = new ClockFrequency();
        customMon.setFrequencyType(Consts.IClockFrequencyType.TYPE_CUSTOM_MON);
        customMon.setName(getString(R.string.clock_frequency_every_mon));
        customMon.setSelected(true);
        frequencies.add(customMon);
        // 每周二
        ClockFrequency customTues = new ClockFrequency();
        customTues.setFrequencyType(Consts.IClockFrequencyType.TYPE_CUSTOM_TUES);
        customTues.setName(getString(R.string.clock_frequency_every_tues));
        customTues.setSelected(true);
        frequencies.add(customTues);
        // 每周三
        ClockFrequency customWed = new ClockFrequency();
        customWed.setFrequencyType(Consts.IClockFrequencyType.TYPE_CUSTOM_WED);
        customWed.setName(getString(R.string.clock_frequency_every_wed));
        customWed.setSelected(true);
        frequencies.add(customWed);
        // 每周四
        ClockFrequency customThur = new ClockFrequency();
        customThur.setFrequencyType(Consts.IClockFrequencyType.TYPE_CUSTOM_THUR);
        customThur.setName(getString(R.string.clock_frequency_every_thur));
        customThur.setSelected(true);
        frequencies.add(customThur);
        // 每周五
        ClockFrequency customFri = new ClockFrequency();
        customFri.setFrequencyType(Consts.IClockFrequencyType.TYPE_CUSTOM_FRI);
        customFri.setName(getString(R.string.clock_frequency_every_fri));
        customFri.setSelected(true);
        frequencies.add(customFri);
        // 每周六
        ClockFrequency customSat = new ClockFrequency();
        customSat.setFrequencyType(Consts.IClockFrequencyType.TYPE_CUSTOM_SAT);
        customSat.setName(getString(R.string.clock_frequency_every_sta));
        customSat.setSelected(true);
        frequencies.add(customSat);
        // 每周日
        ClockFrequency customSun = new ClockFrequency();
        customSun.setFrequencyType(Consts.IClockFrequencyType.TYPE_CUSTOM_SUN);
        customSun.setName(getString(R.string.clock_frequency_every_sun));
        customSun.setSelected(true);
        frequencies.add(customSun);

        return frequencies;
    }

    private void setListener() {
        mSmsRemind.setOnCheckedChangeListener(new SwitchButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(SwitchButton view, boolean isChecked) {
                ToastShow.show(ClockFrequencyActivity.this, isChecked ? "Opened" : "Closed");
            }
        });
    }
}
